import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class ChangePasswordGUI extends JPanel implements ActionListener{
	private JPanel passPanel, row1, row2, row3, row4;
	private JLabel currentPassword, newPassword, verifyNewPassword;
	private JPasswordField cpText, npText, vnpText;
	private JButton submitButton, cancelButton;
	
	public ChangePasswordGUI(){

		
		passPanel = new JPanel(new GridLayout(4,1));
		row1 = new JPanel(new FlowLayout());
		row2 = new JPanel(new FlowLayout());
		row3 = new JPanel(new FlowLayout());
		row4 = new JPanel(new FlowLayout());
		
		submitButton = new JButton("SUBMIT");
		cancelButton = new JButton("CANCEL");
		
		currentPassword = new JLabel("Enter Password");
		newPassword = new JLabel("Enter New Password");
		verifyNewPassword = new JLabel("Verify New Password");
		
		cpText = new JPasswordField();
		npText = new JPasswordField();
		vnpText = new JPasswordField();
		
		cpText.setEchoChar('*');
		npText.setEchoChar('*');
		vnpText.setEchoChar('*');
		
		cpText.setPreferredSize(new Dimension(150,20));
		npText.setPreferredSize(new Dimension(150,20));
		vnpText.setPreferredSize(new Dimension(150,20));
		
		row1.add(currentPassword);
		row1.add(cpText);
		row2.add(newPassword);
		row2.add(npText);
		row3.add(verifyNewPassword);
		row3.add(vnpText);
		row4.add(cancelButton);
		row4.add(submitButton);
		
		passPanel.add(row1);
		passPanel.add(row2);
		passPanel.add(row3);
		passPanel.add(row4);

		this.add(passPanel);
		
		submitButton.addActionListener(this);
		cancelButton.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == submitButton){
			GUIMaster gm = GUIMaster.getInstance();
			SystemController sc = gm.getWorkhorse();
			// if current Password is not equal to the new password and new pass equals verify new pass
			if(!cpText.getText().equals(npText.getText()) && npText.getText().equals(vnpText.getText())){
				// allow the change in the Database

				// amm - I'm manually inserting lou because there is no login field just yet
				if (gm.getWorkhorse().submitPasswordChange(cpText.getText(), npText.getText())){
					JOptionPane.showMessageDialog(null, "Your Password Has Been Successfully Changed");
					gm.passChangeSuccessful();
				}
				else
					JOptionPane.showMessageDialog(null, "User could not be validated. Password not changed.");
				
			}
			if(cpText.getText().equals(npText.getText()) && npText.getText().equals(vnpText.getText())){
				JOptionPane.showMessageDialog(null, "The new password is already your password!");
				gm.passChangeSuccessful();
			}
			if(!npText.getText().equals(vnpText.getText())){
				JOptionPane.showMessageDialog(null, "The newly entered passwords are inconsistent. Try again!");
			}
		}
		else if(e.getSource() == cancelButton){
			GUIMaster gm = GUIMaster.getInstance();
			gm.passChangeCancelled();
		}
	}
	
	public static void main(String[] args){
		JFrame f = new JFrame();
		f.add(new ChangePasswordGUI());
		f.setSize(500, 500);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}


	
}
